﻿/*
 * Created by SharpDevelop.
 * User: amb
 * Date: 30/05/2014
 * Time: 14:35
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using JdFormsDll;
using System.ComponentModel;
using System.IO;

namespace JdForms
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : MyForm
    {
        public MainForm()
        {
            InitializeComponent();
        }

        
        private MyFormClassesGenerator ClassesGenerator { get; set; }
        private bool EditMe = true;
        private bool SelectAlls = false;
       
        private void MainForm_Load(object sender, EventArgs e)
        {
            this.Decorator = new MyFormDecorator(this,@"..\..\..\");
            Decorator.OnLoad += new Load(Decorator_OnLoad);
            Decorator.OnSave += new Save(Decorator_OnSave);
            this.ClassesGenerator = new MyFormClassesGenerator(this.Decorator);
            this.LoadFiles();
            label1.Text = "Decorator from Design";
        }

        private void Decorator_OnLoad(object sender, DecoratorEventArgs e)
        {
            if (this.EditMe) label1.Text = "Decorator from " + Path.GetFileNameWithoutExtension(e.FileName);
            this.LoadFiles();
        }

        private void Decorator_OnSave(object sender, DecoratorEventArgs e)
        {
            if (this.EditMe) label1.Text = "Decorator from " + Path.GetFileNameWithoutExtension(e.FileName);
            this.LoadFiles();
        }

        private void LoadFiles()
        {
            this.clbFiles.Items.Clear();
            this.clbFiles.Items.AddRange(this.ClassesGenerator.Files());
            this.SelectDecoratorFiles();
            this.label2.ForeColor = this.BackColor.BlackAndWhite();
            this.Refresh();
        }

        private void SelectDecoratorFiles()
        {
            for (int i = 0; i < clbFiles.Items.Count; ++i)
                clbFiles.SetItemChecked(i, SelectAlls);
        }

        private void SaveDll(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                Alarm("Not Decorator files selected !!!");
                return;
            }
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.InitialDirectory = this.Decorator.MyDllPath;
                sfd.CheckFileExists = false;
                sfd.CheckPathExists = true;
                sfd.OverwritePrompt = true;
                sfd.DefaultExt = "dll";
                sfd.Filter = "JdForm Decorator Dll (*.dll)|*.dll";
                sfd.FilterIndex = 1;
                sfd.FileName = "Noname";
                sfd.SupportMultiDottedExtensions = false;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    string result = CSharpCompiler.Compile(value, sfd.FileName);
                    if (!string.IsNullOrEmpty(result))
                        Info(string.Format("Created assembly file '{0}'", result));
                    else Alarm("Impossible to create assembly file !!!");
                }
                else Info("Operation Cancelled by User !!!");
            }
        }

        private void Alarm(string value)
        {
            MyFormMessageBox.Show(this, value, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Stop);
        }

        private void Info(string value)
        {
            MyFormMessageBox.Show(this, value, this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void SaveCSharpClass(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                Alarm("Not Decorator files selected !!!");
                return;
            }
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.InitialDirectory = this.Decorator.MyCSharpPath;
                sfd.CheckFileExists = false;
                sfd.CheckPathExists = true;
                sfd.OverwritePrompt = true;
                sfd.DefaultExt = "cs";
                sfd.Filter = "JdForm Decorator C# Class (*.cs)|*.cs";
                sfd.FilterIndex = 1;
                sfd.FileName = "Noname";
                sfd.SupportMultiDottedExtensions = false;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    File.WriteAllText(sfd.FileName, value);
                    Info(string.Format("Saved file '{0}'", sfd.FileName));
                }
                else Info("Operation Cancelled by User !!!");
            }
        }

        private void SaveVBClass(string value)
        {
            if (string.IsNullOrEmpty(value))
            {
                Alarm("Not Decorator files selected !!!");
                return;
            }
            using (SaveFileDialog sfd = new SaveFileDialog())
            {
                sfd.InitialDirectory = this.Decorator.MyVbPath;
                sfd.CheckFileExists = false;
                sfd.CheckPathExists = true;
                sfd.OverwritePrompt = true;
                sfd.DefaultExt = "cs";
                sfd.Filter = "JdForm Decorator VB Class (*.vb)|*.vb";
                sfd.FilterIndex = 1;
                sfd.FileName = "Noname";
                sfd.SupportMultiDottedExtensions = false;
                if (sfd.ShowDialog() == DialogResult.OK)
                {
                    File.WriteAllText(sfd.FileName, value);
                    Info(string.Format("Saved file '{0}'", sfd.FileName));
                }
                else Info("Operation Cancelled by User !!!");
            }
        }

        private void miLoad_Click(object sender, EventArgs e)
        {
            this.EditMe = true;
            string[] files = this.Decorator.GetFiles();
            if (files.Length > 0)
                this.Decorator.Load(files[0]);
        }

        private void miOpen_Click(object sender, EventArgs e)
        {
            this.EditMe = true;
            if (this.Decorator.Open() == DialogResult.Cancel)
                MyFormMessageBox.Show(this, "Operation Cancelled by User !!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.LoadFiles();
        }

        private void miSave_Click(object sender, EventArgs e)
        {
            this.EditMe = true;
            if (this.Decorator.Save() == DialogResult.Cancel)
                MyFormMessageBox.Show(this, "Operation Cancelled by User !!", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Information);
            this.LoadFiles();
        }

        private void miOpenDecoratorFolder_Click(object sender, EventArgs e)
        {
            this.Decorator.OpenDecoratorFolder();
            this.LoadFiles();
        }
        
        		
		void MiOpenDllFolder_Click(object sender, EventArgs e)
		{
			this.Decorator.OpenDllFolder();
		}
		
		void MiOpenCsharpFolder_Click(object sender, EventArgs e)
		{
			this.Decorator.OpenCsharpFolder();
		}
		
		void MiOpenVbFolder_Click(object sender, EventArgs e)
		{
			this.Decorator.OpenVbFolder();
		}

        private void miShow_Click(object sender, EventArgs e)
        {
            this.EditMe = false;
            this.Decorator.ShowDialog();
            this.LoadFiles();
        }

        private void miShowthis_Click(object sender, EventArgs e)
        {
            this.EditMe = true;
            this.Decorator.ShowDialog(this);
            this.LoadFiles();
        }

        private void miCSharpClass_Click(object sender, EventArgs e)
        {
            List<string> files = new List<string>();
            foreach (var v in clbFiles.CheckedItems)
                files.Add(v.ToString());
            this.SaveCSharpClass(this.ClassesGenerator.CreateCSharpClass(files));
        }

        private void miVBClass_Click(object sender, EventArgs e)
        {
            List<string> files = new List<string>();
            foreach (var v in clbFiles.CheckedItems)
                files.Add(v.ToString());
            this.SaveVBClass(this.ClassesGenerator.CreateVBClass(files));
        }

        private void miDll_Click(object sender, EventArgs e)
        {
            List<string> files = new List<string>();
            foreach (var v in clbFiles.CheckedItems)
                files.Add(v.ToString());
            this.SaveDll(this.ClassesGenerator.CreateCSharpClass(files, true));
        }

        private void miReset_Click(object sender, EventArgs e)
        {
            this.Decorator.RestoreProperties();
            label1.Text = "Decorator from Design";
            this.Invalidate();
        }

        private void miSelect_Click(object sender, EventArgs e)
        {
            ToolStripButton tsb = (ToolStripButton)sender;
            if (tsb == null) return;
            this.SelectAlls = tsb.Checked;
            if (tsb.Checked) tsb.Text = "Unselect Alls";
            else tsb.Text = "Select Alls";
            this.SelectDecoratorFiles();
        }
		
    }
}

